package com.lmds.sprindemo1.controller;

import com.lmds.sprindemo1.controller.event.DemoPublisher;
import com.lmds.sprindemo1.po.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.IOUtils;

import javax.xml.soap.SAAJResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@PropertySource("classpath:test.properties")
public class IndexController {

    @Value("${test.value.name}")
    String value;  //配置文件，默认加载application.properties

    @Value("abc")
    String valueStr;  // 注入普通字符串

    @Value("${test.value}")
    String valueAnother;  // 注入配置文件，属性名相同自定义属性文件。
    // 自定义属性文件通过@PropertySource加载。@PropertySource可以同时加载多个文件，也可以加载单个文件。
    // 如果相同第一个属性文件和第二属性文件存在相同key，则最后一个属性文件里的key启作用，application.properties优先级最高。


    @Value("#{systemProperties['os.name']}")
    private String systemPropertiesName; // 注入操作系统属性

    @Value("#{ T(java.lang.Math).random() * 100.0 }")
    private double randomNumber; //注入表达式结果

    @Value("#{beanInject.another}")
    private String fromAnotherBean; // 注入其他Bean属性：注入beanInject对象的属性another，类具体定义见下面

    @Value("classpath:com/hry/spring/configinject/config.txt")
    private Resource resourceFile; // 注入文件资源

    @Value("http://www.baidu.com")
    private Resource testUrl; // 注入URL资源


    @Autowired
    DemoPublisher demoPublisher;

    @RequestMapping({"/","index.do"})
    public String index(ModelMap modelMap){
        modelMap.addAttribute("txe","测试内容");

        List<Goods> goodsList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Goods goods = new Goods();
            goods.setId(i);
            goods.setName("测试商品"+ i);
            goods.setPrice(89.0 + i) ;
            goods.setImg("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3811062792,3685021368&fm=27&gp=0.jpg");
            goods.setContent("库尔勒香梨");
            goodsList.add(goods);
        }
        modelMap.addAttribute("list",goodsList);
        return "index";
    }

    @RequestMapping("/test")
    @ResponseBody
    public String tetst(){

        demoPublisher.publish("visit tetst on :"+ new Date());
        String returnStr = "";
        returnStr +="Hello Spring Boot !<br>" ;
        returnStr +="value:" + value + "<br>" ;
        returnStr +="valueAnother:" + valueAnother + "<br>" ;
        returnStr +="valueStr:" + valueStr + "<br>" ;
        returnStr +="systemPropertiesName:" + systemPropertiesName + "<br>" ;
        returnStr +="fromAnotherBean:" + fromAnotherBean + "<br>" ;
        try {
            returnStr +="testUrl:" + testUrl.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  returnStr;
    }
}
